package com.niuwa.p2p.service.interview.impl;

import java.util.List;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.DictConstant;
import com.niuwa.p2p.dao.channel.LoanClaimAuditDao;
import com.niuwa.p2p.dao.channel.LoanRelativeFileDao;
import com.niuwa.p2p.dao.interview.InterviewDao;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.entity.channel.LoanClaimAudit;
import com.niuwa.p2p.entity.channel.LoanRelativeFile;
import com.niuwa.p2p.exception.BusinessException;
import com.niuwa.p2p.service.interview.IInterviewService;
import com.niuwa.p2p.vo.interview.InterviewVo;

/**
 * 面签管理Service
 * 
 * @author houdianbo
 * @Date 2015-04-23
 */
@Service
public class InterviewServiceImpl implements IInterviewService {
	@Autowired
	private InterviewDao interviewDao;
	@Autowired
	private LoanClaimAuditDao loanClaimAuditDao;
	@Autowired
	private LoanRelativeFileDao loanRelativeFileDao;

	@Override
	public PageBean<InterviewVo> selectInterviewByParam(
			Map<String, Object> paramMap) {
		assert paramMap.get(PageQuery.PAGEQUERY) != null;
		String projectNo = String.valueOf(paramMap.get("projectNo"));
		if (projectNo != null) {
			projectNo = projectNo.trim().replaceAll("%", "\\\\%")
					.replaceAll("_", "\\\\_");
		}
		String borrowerName = String.valueOf(paramMap.get("borrowerName"));
		if (borrowerName != null) {
			borrowerName = borrowerName.trim().replaceAll("%", "\\\\%")
					.replaceAll("_", "\\\\_");
		}
		// 总条数
		int totalCount = interviewDao.getInterviewCountByParam(paramMap);
		PageBean<InterviewVo> page = new PageBean<InterviewVo>(totalCount);
		// 当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());
		page.setRows(interviewDao.selectInterviewByParam(paramMap));
		return page;
	}

	public void setInterviewDao(InterviewDao interviewDao) {
		this.interviewDao = interviewDao;
	}

	public void setLoanClaimAuditDao(LoanClaimAuditDao loanClaimAuditDao) {
		this.loanClaimAuditDao = loanClaimAuditDao;
	}

	@Override
	public boolean updateViewAuditStatus(Map<String, Object> paramMap,
			Long operator, String userName) throws BusinessException {
		assert paramMap.get("version") != null;
		assert "VIEW_PASS".equals(paramMap.get("auditStatus"))
				|| "REJECT_VIEW".equals(paramMap.get("auditStatus"));
		paramMap.put("version",
				Long.parseLong(paramMap.get("version").toString()));
		this.interviewDao.updateLoanClaimAuditStatusByVersion(paramMap);

		LoanClaimAudit record = new LoanClaimAudit();
		record.setOperator(operator);
		record.setUserName(userName);
		if ("VIEW_PASS".equals(paramMap.get("auditStatus"))) {
			record.setOperateType(DictConstant.LOAN_CLAIM_TYPE.VIEW_PASS
					.getCode());
		} else {
			record.setOperateType(DictConstant.LOAN_CLAIM_TYPE.VIEW_REJECT
					.getCode());
		}

		record.setProjectNo(paramMap.get("projectNo").toString());
		record.setRemark((String) paramMap.get("remark"));
		record.setCreateTime(new Date());
		this.loanClaimAuditDao.insert(record);
		return true;
	}

	public List<LoanRelativeFile> getLoanRelativeFile(String projectNo) {
		return this.loanRelativeFileDao
				.selectGenerateFileByProjectNo(projectNo);
	}

	public LoanClaimAudit getLoanClaimAudit(Map<String, Object> paramMap) {
		paramMap.remove(PageQuery.PAGEQUERY);
		List<LoanClaimAudit> list = loanClaimAuditDao
				.selectLoanClaimAuditListByParams(paramMap);
		LoanClaimAudit lca = null;
		if (list != null) {
			for (LoanClaimAudit audit : list) {
				if (DictConstant.LOAN_CLAIM_TYPE.VIEW_REJECT.getCode().equals(
						audit.getOperateType())) {
					lca = audit;
					break;
				}
			}
		}
		return lca;
	}
}
